home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJSRC111.ZIP / go32 / exe2coff.c < prev    next >
C/C++ Source or Header  |  1993-07-24  |  2KB  |  92 lines

  1. /* This is file EXE2AOUT.C */
  2. /*
  3. ** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <sys/stat.h>
  19. #include <string.h>
  20. #include <io.h>
  21.  
  22.  
  23. void exe2aout(char *fname)
  24. {
  25.   unsigned short header[3];
  26.   int ifile;
  27.   int ofile;
  28.   char buf[4096];
  29.   int rbytes;
  30.   ifile = open(fname, O_RDONLY|O_BINARY);
  31.   if (ifile < 0)
  32.   {
  33.     perror(fname);
  34.     return;
  35.   }
  36.   read(ifile, header, sizeof(header));
  37.   if (header[0] == 0x5a4d)
  38.   {
  39.     long header_offset = (long)header[2]*512L;
  40.     if (header[1])
  41.       header_offset += (long)header[1] - 512L;
  42.     lseek(ifile, header_offset, 0);
  43.     header[0] = 0;
  44.     read(ifile, header, sizeof(header));
  45.     if ((header[0] != 0x010b) && (header[0] != 0x014c))
  46.     {
  47.       fprintf(stderr, "%s does not have an a.out file appended to it\n", fname);
  48.       exit(1);
  49.     }
  50.     lseek(ifile, header_offset, 0);
  51.   }
  52.   else
  53.   {
  54.     fprintf(stderr, "%s is not an .EXE file\n", fname);
  55.     exit(1);
  56.   }
  57.   
  58.   *strrchr(fname, '.') = 0;
  59.   ofile = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
  60.   if (ofile < 0)
  61.   {
  62.     perror(fname);
  63.     return;
  64.   }
  65.   
  66.   while ((rbytes=read(ifile, buf, 4096)) > 0)
  67.   {
  68.     int wb = write(ofile, buf, rbytes);
  69.     if (wb < 0)
  70.     {
  71.       perror(fname);
  72.       break;
  73.     }
  74.     if (wb < rbytes)
  75.     {
  76.       fprintf(stderr, "%s: disk full\n", fname);
  77.       exit(1);
  78.     }
  79.   }
  80.   close(ifile);
  81.   close(ofile);
  82. }
  83.  
  84. int main(int argc, char **argv)
  85. {
  86.   int i;
  87.   for (i=1; i<argc; i++)
  88.     exe2aout(argv[i]);
  89.   return 0;
  90. }
  91.  
  92.